package net.ib.happy2v.contact;

import java.util.ArrayList;

import net.ib.happy2v.R;
import android.app.Activity;
import android.content.ContentProviderOperation;
import android.content.Context;
import android.content.OperationApplicationException;
import android.os.Bundle;
import android.os.RemoteException;
import android.provider.ContactsContract;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class AddTestNumber extends Activity{
	private Context mContext;
	private int mCount;
	private TextView tv;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.layout_contact_add_test_number);
		mContext = this;
		
		mCount = 0;
		
		Button btn = (Button) findViewById(R.id.contact_add_test_number_btn);
		tv = (TextView) findViewById(R.id.text_count);

		Thread thread = new Thread(new Runnable() {
			public void run() {
				while(true){
					if(mCount > 9999){
						break;
					}
					
//					try {
//						wait(5000);
//					} catch (InterruptedException e1) {
//						Log.e("ATOM", "run()" + e1);
//						e1.printStackTrace();
//					}
					for(int i=mCount; i<mCount+50; i++){
						String familyName = "Number";
						String givenName = "mtalk";
						String number = "010-0000-";
						
						if(i < 0){
							continue;
						}if(i < 10){
							number = number + "000" + i;// + "-2189";
						}else if(i < 100){
							number = number + "00" + i;// + "-2189";
						}else if(i < 1000){
							number = number + "0" + i;// + "-2189";
						}else{
							number = number + i;// + "-2189";
						}
						familyName = number + " : ";
						Log.i("ADDCONTACTS", "ADD : " +  familyName);
						ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
						
						// 계정 내용 기록
						ops.add(ContentProviderOperation.newInsert(ContactsContract.RawContacts.CONTENT_URI)
								.withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, "com.google")
								.withValue(ContactsContract.RawContacts.ACCOUNT_NAME, "ibmntalk@gmail.com")
								.build());
						
						// 
						ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
								.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
								.withValue(ContactsContract.Data.MIMETYPE,
										ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE)
								.withValue(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME, givenName)
								.withValue(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME, familyName)
								.build());
						ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
								.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
								.withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
								.withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, number)
								.withValue(ContactsContract.CommonDataKinds.Phone.TYPE, ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE)
								.withValue(ContactsContract.CommonDataKinds.Phone.LABEL, "")
								.build());
								
						try {
							mContext.getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
						} catch (RemoteException e) {
							Log.e("ATOM", "AddTestNumber : " + e);
							e.printStackTrace();
						} catch (OperationApplicationException e) {
							Log.e("ATOM", "AddTestNumber : " + e);
							e.printStackTrace();
						}
					}
					mCount = mCount+50;
					tv.setText(String.valueOf(mCount));
				}
			}
		});
		thread.run();
		
//		btn.setOnClickListener(new OnClickListener() {
//			public void onClick(View v) {
//				for(int i=mCount; i<mCount+10; i++){
//					String familyName = "Number";
//					String givenName = "(mtalk)";
//					String number = "010-";
//					familyName = "(" + i + ")";
//					
//					if(i < 0){
//						continue;
//					}if(i < 10){
//						number = number + "000" + i + "-2189";
//					}else if(i < 100){
//						number = number + "00" + i + "-2189";
//					}else if(i < 1000){
//						number = number + "0" + i + "-2189";
//					}else{
//						number = number + i + "-2189";
//					}
//					
//					ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
//					
//					// 계정 내용 기록
//					ops.add(ContentProviderOperation.newInsert(ContactsContract.RawContacts.CONTENT_URI)
//							.withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, "com.google")
//							.withValue(ContactsContract.RawContacts.ACCOUNT_NAME, "ibmntalk@gmail.com")
//							.build());
//					
//					// 
//					ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
//							.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
//							.withValue(ContactsContract.Data.MIMETYPE,
//									ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE)
//							.withValue(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME, givenName)
//							.withValue(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME, familyName)
//							.build());
//					ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
//							.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
//							.withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
//							.withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, number)
//							.withValue(ContactsContract.CommonDataKinds.Phone.TYPE, ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE)
//							.withValue(ContactsContract.CommonDataKinds.Phone.LABEL, "")
//							.build());
//							
//					try {
//						mContext.getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
//					} catch (RemoteException e) {
//						Log.e("ATOM", "AddTestNumber : " + e);
//						e.printStackTrace();
//					} catch (OperationApplicationException e) {
//						Log.e("ATOM", "AddTestNumber : " + e);
//						e.printStackTrace();
//					}
//				}
//				mCount = mCount+10;
//				tv.setText(String.valueOf(mCount));
//			}
//		});
	}
}
